//
//  Dialog.swift
//  PopupDemo
//
//  Created by zhuxuhong on 2020/11/1.
//

import UIKit
import BWToolKit

extension Dialog {
    open class Appearance {
        public typealias TextConfig = Dialog.Text.Config
        open class Action {
            public var height: CGFloat = 44
            public var defaultConfig: TextConfig = .init(color: .black, font: .systemFont(ofSize: 17))
            public var cancelConfig: TextConfig = .init(color: .systemBlue, font: .systemFont(ofSize: 17, weight: .medium))
            public var destructiveConfig: TextConfig = .init(color: .systemRed, font: .systemFont(ofSize: 17))
            public var primaryConfig: TextConfig = .init(color: .systemGreen, font: .systemFont(ofSize: 17))
            public var maxCountOneLine = 2
            public init(){}
        }
        open class Text {
            public var areaInsets = UIEdgeInsets(top: 16, left: 16, bottom: 20, right: 16)
            public var areaSpacing: CGFloat = 10
            public var titleConfig: TextConfig = .init(font: .systemFont(ofSize: 16, weight: .medium), numberOfLines: 1, lineHeightMultiple: 1.5)
            public var messageConfig: TextConfig = .init(lineHeightMultiple: 1.5)
            public init(){}
        }
        open class ContentView {
            public var width: CGFloat = 270
            public var maxHeight: CGFloat {
                if UIDevice.current.orientation.isLandscape {
                    return UIScreen.main.bounds.height - 34 - 40
                }
                return UIScreen.main.bounds.height - 88 - 40
            }
            public var cornerRadius: CGFloat = 16
            public var backgroundColor: UIColor? = .white
            public init(){}
        }
        open class Separator {
            public var color: UIColor? = UIColor.lightGray.withAlphaComponent(0.3)
            public var width: CGFloat = 1
            public init(){}
        }
        
        public var contentView = ContentView()
        public var separator = Separator()
        public var text = Text()
        public var action = Action()
        
        public init(){}
    }
}

open class Dialog {
    public static var appearance = Appearance()
    
    public var title: Text?
    public var message: Text?
    public var actions: [Action] = []
    var textFields: [TextField] = []
    
    public init(title: Dialog.Text? = nil,
                message: Dialog.Text? = nil,
                actions: [Action] = [])
    {
        self.title = title
        self.message = message
        self.actions = actions
    }
}

extension Dialog {
    @discardableResult
    public static func show(
        title: String? = nil,
        message: String? = nil,
        actions: [Action] = [],
        maskViewCancellable: Bool = false,
        completion: (()->Void)? = nil
    ) -> DialogVC {
        var titleText: Dialog.Text? = nil
        var messageText: Dialog.Text? = nil
        if let string = title {
            titleText = .init(string: string, config: Dialog.appearance.text.titleConfig)
        }
        if let string = message {
            messageText = .init(string: string, config: Dialog.appearance.text.messageConfig)
        }
        
        let vc: DialogVC = .init(.init(title: titleText, message: messageText, actions: actions))
        vc.canMaskTouchedDismissable = (title==nil && message==nil) || actions.isEmpty ? true : false
        Jumper.jump(.present(vc, animated: false))
        
        return vc
    }
}

extension Dialog {
    open class Text {
        public var string: String?
        public var config: Text.Config!
        
        public init(string: String?, config: Text.Config) {
            self.string = string
            self.config = config
        }
    }
}


extension Dialog {
    open class TextField {
        
    }
}

extension Dialog {
    public class Action {
        public typealias Handler = ()->Void
        public var title: String!
        public var style: Style!
        public var handler: Handler?
        /// 是否允许点击完按钮后弹框消失
        public var closeable: Bool!
        public init(title: String,
                    style: Style? = .default,
                    closeable: Bool = true,
                    handler: Handler? = nil) {
            self.title = title
            self.style = style
            self.closeable = closeable
            self.handler = handler
        }
    }
}

extension Dialog.Action {
    public enum Style {
        case `default`
        case cancel
        case primary
        case destructive
        case custom(Dialog.Text.Config)
        
        public var config: Dialog.Text.Config
        {
            switch self {
            case .`default`:
                return Dialog.appearance.action.defaultConfig
            case .cancel:
                return Dialog.appearance.action.cancelConfig
            case .destructive:
                return Dialog.appearance.action.destructiveConfig
            case .primary:
                return Dialog.appearance.action.primaryConfig
            case .custom(let config):
                return config
            }
        }
    }
}


extension Dialog.Text {
    enum Style {
        case title
        case message
        case custom(Dialog.Text.Config)
    }
}

extension Dialog.Text {
    open class Config {
        public var color: UIColor?
        public var font: UIFont!
        public var alignment: NSTextAlignment!
        public var numberOfLines: Int!
        public var lineHeightMultiple: CGFloat?
        
        public init(color: UIColor? = .black,
                    font: UIFont = .systemFont(ofSize: 14),
                    alignment: NSTextAlignment = .center,
                    numberOfLines: Int = 0,
                    lineHeightMultiple: CGFloat? = nil)
        {
            self.color = color
            self.font = font
            self.alignment = alignment
            self.numberOfLines = numberOfLines
            self.lineHeightMultiple = lineHeightMultiple
        }
    }
}


